﻿
layui.use(['form', 'layer'], function () {
    var form = layui.form,
        layer = layui.layer;

    $(function () {
        getHomeStaInfo();
        loadMap();
    });
});

//获取统计信息
function getHomeStaInfo() {
    app.get('/Business/Home/Home.ashx', {
        ActionType: 'HomeStaInfo',
        rmd: Math.random()
    }, function (data) {
        if (data) {
            //console.log(data);
            $('#tbProjectCount').text(data.ProjectCount);
            $('#tbPersonCount').text(data.PersonCount);
            $('#tbTodayLoginCount').text(data.TodayLoginCount);
            $('#tbTotalLoginCount').text(data.TotalLoginCount);
        }
    });
}

//项目地图
function loadMap() {
    var actualLng = parseFloat($("#tbCityMapPos").val().split(',')[0], 10);
    var actualLat = parseFloat($("#tbCityMapPos").val().split(',')[1], 10);

    var bdMap = new BMap.Map('map', { enableMapClick: false });
    bdMap.enableScrollWheelZoom(true); //开启鼠标滚轮缩放
    bdMap.disableDoubleClickZoom();
    //bdMap.centerAndZoom(new BMap.Point(actualLng, actualLat), 12);

    var onlineIcon = new BMap.Icon("/resources/images/OnlineDevice.png", new BMap.Size(22, 22), { offset: new BMap.Size(22, 22), imageSize: new BMap.Size(22, 22) });
    //var offlineIcon = new BMap.Icon("/resources/images/OfflineDevice.png", new BMap.Size(30, 38), { offset: new BMap.Size(15, 15), imageSize: new BMap.Size(22, 22) });
    //var warningIcon = new BMap.Icon("/resources/images/Warning.png", new BMap.Size(22, 22), { offset: new BMap.Size(22, 22), imageSize: new BMap.Size(22, 22) });
    //var alarmIcon = new BMap.Icon("/resources/images/Failed.png", new BMap.Size(22, 22), { offset: new BMap.Size(22, 22), imageSize: new BMap.Size(22, 22) });

    $.getJSON("/Business/Home/Home.ashx",
        {
            PostType: "get",
            ActionType: "ProjectList",
            Random: Math.random()
        },
        function (projectData, Success) {
            var bPoints = new Array();
            //console.log(projectData);
            if (projectData != null && projectData.length > 0) {
                for (var i = 0; i < projectData.length; i++) {
                    var project = projectData[i];
                    var pointArr = project.Coordinate.split(',');
                    var markerPoint = new BMap.Point(pointArr[0], pointArr[1]);
                    bPoints.push(markerPoint); // 添加到百度坐标数组 用于自动调整缩放级别
                    var markerIcon = onlineIcon;
                    var spanStatus = '<span class="spanBtn successSpan" style="width:30px;">正常</span>';
                    //if (project.SITEGUID2 == null) { //未添加考勤点
                    //    markerIcon = offlineIcon;
                    //    spanStatus = '<span class="spanBtn undoSpan" style="width:42px;">未开始</span>';
                    //}
                    //else if (project.ISSTOP == "1") { //设备离线
                    //    markerIcon = alarmIcon;
                    //    spanStatus = '<span class="spanBtn dangerSpan" style="width:30px;">终止</span>';
                    //}
                    //else if (project.ISSTOP == "2") { //考勤异常
                    //    markerIcon = warningIcon;
                    //    spanStatus = '<span class="spanBtn changeSpan" style="width:30px;">异常</span>';
                    //}
                    //else {
                    //    markerIcon = onlineIcon;
                    //    spanStatus = '<span class="spanBtn successSpan" style="width:30px;">正常</span>';
                    //}

                    var marker = new BMap.Marker(markerPoint, { icon: markerIcon });

                    marker.GUID = project.GUID;

                    marker.ProjectInfo = "工程地址：" + (project.Address == null || project.Address == "null" ? "信息不全" : project.Address) + //工程地址
                        "<br/>施工单位：" + (project.ConstructionUnit == null || project.ConstructionUnit == "null" ? "信息不全" : project.ConstructionUnit) + //施工单位名称
                        "<br/>建设单位：" + (project.BuildUnit == null || project.BuildUnit == "null" ? "信息不全" : project.BuildUnit) + //建设单位名称
                        "<br/>监理单位：" + (project.SupervisionUnit == null || project.SupervisionUnit == "null" ? "信息不全" : project.SupervisionUnit) + //监理单位名称
                        "<br/>";
                    marker.SegmentName = project.ProjectName; //工程名称
                    marker.spanStatus = spanStatus;
                    marker.onclick = function () { showMarkerWindow(this, bdMap); };

                    bdMap.addOverlay(marker);

                }
            }
            var view = bdMap.getViewport(eval(bPoints));
            var mapZoom = view.zoom;
            var centerPoint = view.center;
            bdMap.centerAndZoom(centerPoint, mapZoom);
        });
}
function showMarkerWindow(marker, objMap) {
    // 信息窗口标题
    var searchInfoWindow = null;
    var content = '<div style="margin:0;line-height:20px;padding:2px;padding-bottom:8px;">' +
        '<div style="color:#555555;"><table><tr><td>' + marker.ProjectInfo + '</td></tr></table></div>' + marker.spanStatus +
        '<div id="btnMarkerDetail" style="float:left;margin-top:6px;margin-bottom:6px;background-color:#F9F9F9;cursor: pointer; border:1px solid #75ade4;width:76px;height:22px;padding:2px 0px;margin-left:4px;auto;text-align:center;border-radius: 4px 4px 4px 4px;"><a style="color:#0f66bc; font-weight:bold;fon-size:15px;">项目详情</a></div>' +
        //'<div id="btnAttendance" style="float:left;margin-top:6px;margin-bottom:6px;background-color:#F9F9F9;cursor: pointer; border:1px solid #75ade4;width:76px;height:22px;padding:2px 0px;margin-left:4px;auto;text-align:center;border-radius: 4px 4px 4px 4px;"><a style="color:#0f66bc; font-weight:bold;fon-size:15px;">考勤统计</a></div>' +
        //'<div id="btnVideoMonitor" style="float:left;margin-top:6px;margin-bottom:6px;background-color:#F9F9F9;cursor: pointer; border:1px solid #75ade4;width:76px;height:22px;padding:2px 0px;margin-left:4px;auto;text-align:center;border-radius: 4px 4px 4px 4px;"><a style="color:#0f66bc; font-weight:bold;fon-size:15px;">视频监控</a></div>' +
        '</div>';
    searchInfoWindow = new BMapLib.SearchInfoWindow(objMap, content, {
        title: '<a style="color:#555555;cursor: default;font-weight:bold;fon-size:15px;">' + marker.SegmentName + '</a>',      //标题
        width: 340,
        offset: new BMap.Size(-50, -10),
        searchTypes: [

        ]
    });

    searchInfoWindow.addEventListener("open", function (e) {

        $(".BMapLib_trans").attr("src", "/resources/images/jianjiao.png");
        //fixPNG($(".BMapLib_trans")[0]);
        $('#btnMarkerDetail').click(function () {
            layer.open({
                title: "项目详情",
                type: 2,
                area: ["90%", "600px"],
                scrollbar: false,
                content: "/Business/Project/ProjectView.aspx?GUID=" + marker.GUID
            });
        });

        //$('#btnAttendance').click(function () {
        //    layer.open({
        //        title: "人员考勤统计",
        //        type: 2,
        //        area: ["90%", "620px"],
        //        scrollbar: false,
        //        content: "/Business/Attendance/AttendancePersonSummary.aspx?ProjectGUID=" + marker.GUID
        //    });
        //});

        //$('#btnVideoMonitor').click(function () {
        //    window.open('/Business/VideoMonitor/VideoMonitorList.aspx?ProjectGUID=' + marker.GUID);
        //});
    });
    searchInfoWindow.open(marker);
}
